package com.yksj.healthtalk.comm;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.services.CoreService;
import com.yksj.healthtalk.ui.R;

/**
 * 商品结算 (商品订单界面)
 * 
 * @author Administrator
 * 
 */
public class ShopPayActivity extends BaseFragmentActivity implements
		OnClickListener {

	private WebView wv;
	private Boolean isFirstLoad = true;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.salon_introduction);
		initWidget();
		initData();
	}

	private void initData() {
		// 设置WebView属性，能够执行JavaScript脚本
		wv.getSettings().setJavaScriptEnabled(true);
		wv.postUrl(getIntent().getExtras().getString("addr"),getIntent().getStringExtra("url").getBytes());
		MyWebViewClient myWebView = new MyWebViewClient();
		wv.setWebViewClient(myWebView);
	}

	private void initWidget() {
		initTitle();
		CoreService.actionLogin(this);
		if (getIntent().hasExtra("merchantId")) {
			titleTextV.setText("商品结算");
		}else {
			titleTextV.setText("商户订单详情");
		}
		
		wv = (WebView) findViewById(R.id.wv);
		titleLeftBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;

		default:
			break;
		}
	}

	
	@Override
	public void onBackPressed() {
		if (getIntent().hasExtra("merchantId")) {
			HttpRestClient.doHttpTimeStep(getIntent().getExtras().getString("merchantId"), "20");
			setResult(RESULT_OK, getIntent());
		}
			finish();
	}
	
	private class MyWebViewClient extends WebViewClient {
		private LodingFragmentDialog dialog;

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			if (dialog != null) {
				dialog.dismissAllowingStateLoss();
				dialog = null;
				isFirstLoad = false;
			}
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			if (isFirstLoad) {
				dialog = LodingFragmentDialog.showLodingDialog(
						getSupportFragmentManager(), getResources());
			}
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
		}
	}

	private class MyWebChromeClient extends WebChromeClient {

	}
}
